package com.fg.cloud.factory.controller.product;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.product.vo.ProductMoreUnitVo;
import com.fg.cloud.manager.service.product.ProductMoreUnitService;

/**
 * Description:多单位管理
 * @author around
 * @date 2017年8月20日下午5:34:49
 */
@Controller
@RequestMapping(value="/moreUnit")
public class ProductMoreUnitController extends BaseController{

	@Autowired
	private ProductMoreUnitService moreUnitService; 

	/**
	 * Description:新增多单位
	 * @param request
	 * @param response
	 * @param skuId
	 * @param moreUnit
	 * @author around
	 * @date 2017年8月20日下午6:25:49
	 */
	@RequestMapping(value="/save",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void save(HttpServletRequest request, HttpServletResponse response, 
			Long skuId, String moreUnit){
		if (null == skuId) {
			doResult(response, false, StatusCode.LACKVALUE, null, "未填写经销商信息");
			return;
		}
		if (StringUtils.isEmpty(moreUnit)) {
			doResult(response, false, StatusCode.LACKVALUE, null, "未填写多单位换算信息");
			return;
		}
		try {
			Long facId = getCurrentFactoryUser().getFacId();
			List<ProductMoreUnitVo> moreUnitList = JSON.parseArray(
					StringEscapeUtils.unescapeHtml4(moreUnit), ProductMoreUnitVo.class);
			boolean flag = moreUnitService.insert(facId, skuId, moreUnitList);
			doResult(response, flag, flag ? StatusCode.SAVESUCCESS : StatusCode.SAVEERROR, 
					null, flag ? ResultType.SAVE_SUCCESS : ResultType.SAVE_FAULT);
		} catch (Exception e) {
			doResult(response, false, StatusCode.SAVEERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
	/**
	 * Description:查询
	 * @param response
	 * @param skuId
	 * @author around
	 * @date 2017年8月20日下午6:35:23
	 */
	@RequestMapping(value="/findList",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void findList(HttpServletResponse response, Long skuId){
		if (null == skuId) {
			doResult(response, false, StatusCode.LACKVALUE, null, "未填写经销商信息");
			return;
		}
		try {
			List<ProductMoreUnitVo> moreUnitList = moreUnitService.findList(
					getCurrentFactoryUser().getFacId(), skuId);
			doResult(response, true, StatusCode.FINDSUCCESS, null == moreUnitList ? 
					new ArrayList<ProductMoreUnitVo>() : moreUnitList, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			doResult(response, false, StatusCode.FINDERROR, null, ResultType.FIND_FAULT);
			e.printStackTrace();
		}
	}
	
	/**
	 * Description:批量删除
	 * @param response
	 * @param skuId
	 * @author around
	 * @date 2017年8月20日下午6:34:42
	 */
	@RequestMapping(value="/delete",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void delete(HttpServletResponse response, Long skuId){
		if (null == skuId) {
			doResult(response, false, StatusCode.LACKVALUE, null, "未填写经销商信息");
			return;
		}
		try {
			boolean flag = moreUnitService.deletesBySku(
					getCurrentFactoryUser().getFacId(), skuId);
			doResult(response, flag, flag ? StatusCode.REMOVESUCCESS : StatusCode.REMOVEERROR, 
					null, flag ? ResultType.REMOVE_SUCCESS : ResultType.REMOVE_FAULT);
		} catch (Exception e) {
			doResult(response, false, StatusCode.REMOVEERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
}
